package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车相关接口
 */
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户端购物车相关接口")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品/套餐到购物车
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增购物车")
    public Result<String> addShoppingCartItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("正在添加购物车:{}",shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 删减购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("购物车减少数量")
    public Result<String> subShoppingCartItem(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("减少购物车数量：{}",shoppingCartDTO);
        shoppingCartService.sub(shoppingCartDTO);
        return Result.success();
    }
    /**
     * 列出购物车
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    public Result<List<ShoppingCart>> listShoppingCart(){
        log.info("查询购物车，userId:{}", BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartService.list();
        return Result.success(list);
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> cleanShoppingCart(){
        log.info("清空购物车,userId:{}",BaseContext.getCurrentId());
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }
}
